package org.jeecg.modules.wlhy.hycfg.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.wlhy.hycfg.entity.HyCfg;
import org.jeecg.modules.wlhy.hycfg.service.IHyCfgService;
import org.jeecg.modules.wlhy.utils.HyCfgUtil;
import org.jeecg.modules.wlhy.utils.InitService;
import org.jeecg.modules.wlhy.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

 /**
 * @Description: hy_cfg
 * @Author: jeecg-boot
 * @Date:   2022-09-28
 * @Version: V1.0
 */
@Api(tags="hy_cfg")
@RestController
@RequestMapping("/hycfg/hyCfg")
@Slf4j
public class HyCfgController extends JeecgController<HyCfg, IHyCfgService> {
	@Autowired
	private IHyCfgService hyCfgService;

	@Autowired
	private UserUtil userUtil;

	 @Autowired private InitService initService;

	/**
	 * 分页列表查询
	 *
	 * @param hyCfg
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@ApiOperation(value="hy_cfg-分页列表查询", notes="hy_cfg-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(HyCfg hyCfg,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		if(!userUtil.isSys()){
			return Result.error("无权限操作");
		}
		QueryWrapper<HyCfg> queryWrapper = QueryGenerator.initQueryWrapper(hyCfg, req.getParameterMap());
		Page<HyCfg> page = new Page<HyCfg>(pageNo, pageSize);
		IPage<HyCfg> pageList = hyCfgService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param hyCfg
	 * @return
	 */
	@AutoLog(value = "hy_cfg-添加")
	@ApiOperation(value="hy_cfg-添加", notes="hy_cfg-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody HyCfg hyCfg) {
		String cfgName = hyCfg.getCfgName();
		if(StringUtils.isEmpty(cfgName)){
			return Result.error("配置名称不能为空");
		}
		String cfgCode = hyCfg.getCfgCode();
		if(StringUtils.isEmpty(cfgCode)){
			return Result.error("配置编码不能为空");
		}

		if(!userUtil.isSys()){
			return Result.error("无权限操作");
		}

		if(hyCfgService.checkCodeExist(cfgCode)){
			return Result.error("配置编码已存在");
		}
		hyCfgService.save(hyCfg);
		return Result.OK("添加成功！");
	}

	 @GetMapping(value = "/RefreshCfg")
	 public Result<?> queryPageList(){
		 if(!userUtil.isSys()){
			 return Result.error("无权限操作");
		 }
		 HyCfgUtil.init();
		 initService.setPayCenterCfg();
		 return Result.OK("刷新成功！");
	 }

	
	/**
	 *  编辑
	 *
	 * @param hyCfg
	 * @return
	 */
	@AutoLog(value = "hy_cfg-编辑")
	@ApiOperation(value="hy_cfg-编辑", notes="hy_cfg-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody HyCfg hyCfg) {
		String cfgName = hyCfg.getCfgName();
		if(StringUtils.isEmpty(cfgName)){
			return Result.error("配置名称不能为空");
		}
		String cfgCode = hyCfg.getCfgCode();
		if(StringUtils.isEmpty(cfgCode)){
			return Result.error("配置编码不能为空");
		}

		if(!userUtil.isSys()){
			return Result.error("无权限操作");
		}
		if(hyCfgService.checkCodeExistOutId(cfgCode,hyCfg.getId())){
			return Result.error("配置编码已存在");
		}

		hyCfgService.updateById(hyCfg);
		return Result.OK("编辑成功!");
	}

	//针对巨睿提出派单显示
	 @GetMapping("/getIsShowDriver")
	 public Result<?> getIsShowDriver(){
		 HyCfg config = hyCfgService.getByCode("driver_pay");
		 if (config!=null&&config.getCfgValue().equals("1")){
		 	return Result.OK();
		 }else {
		 	return Result.error("错误");
		 }
	 }
	


}
